package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.QuestionAnswer;
import cn.wolfcode.luowowo.comment.query.QuestionAnswerQuery;
import cn.wolfcode.luowowo.comment.repository.QuestionAnswerRepository;
import cn.wolfcode.luowowo.comment.service.IQuestionAnswerService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.*;

@Service
public class QuestionAnswerServiceImpl implements IQuestionAnswerService {

    @Autowired
    private QuestionAnswerRepository repository;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(QuestionAnswer answer) {

        answer.setCreateTime(new Date());
        repository.save(answer);
    }

    @Override
    public Page query(QuestionAnswerQuery qo) {
        //拼接条件 where questionId = xxx;
        Query query = new Query();
        if(qo.getQuestionId() != -1L){
            query.addCriteria(Criteria.where("questionId").is(qo.getQuestionId()));
        }
        //查询总条数
        long count = mongoTemplate.count(query, QuestionAnswer.class);
        //判断条数是否为0
        if(count == 0){
            return Page.empty();
        }
        //查询分页结果集
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"thumbupnum");
        List<QuestionAnswer> list = mongoTemplate.find(query,QuestionAnswer.class);
        //排序
        Collections.sort(list, new Comparator<QuestionAnswer>() {
            @Override
            public int compare(QuestionAnswer qa1, QuestionAnswer qa2) {
                int ret = qa2.getThumbupnum() - qa1.getThumbupnum();
                return ret;
            }
        });
        //取前十个
        if(list.size() > qo.getPageSize()){
            list = list.subList(0,qo.getPageSize());
        }
        return new PageImpl(list,pageable,count);

    }
    /*
    public Page page(Integer currentPage,Integer pageSize,String...field){
        //查询分页结果集
        Pageable pageable = PageRequest.of(currentPage-1,pageSize, Sort.Direction.DESC,field);
        List<QuestionAnswer> list = mongoTemplate.find(query,QuestionAnswer.class);
        //排序
        Collections.sort(list, new Comparator<QuestionAnswer>() {
            @Override
            public int compare(QuestionAnswer qa1, QuestionAnswer qa2) {
                int ret = qa2.getThumbupnum() - qa1.getThumbupnum();
                return ret;
            }
        });
        //取前十个
        if(list.size() > pageSize){
            list = list.subList(0,pageSize);
        }
        return new PageImpl(list,pageable,count);
    }
    */

    @Override
    public Page queryForList(QuestionAnswerQuery qo,List<Long> qIds) {
        //设置查询条件
        QuestionAnswerQuery qaq = new QuestionAnswerQuery();
        List<QuestionAnswer> questionAnswers = new ArrayList<>();
        long count = 0L;
        Pageable pageable = null;
        for (Long qId : qIds) {
            //通过问题id分组查询
            qaq.setQuestionId(qId);
            Page query = this.query(qaq);
            List<QuestionAnswer> list = query.getContent();
            if(list != null && list.size()!=0){
                count = list.size();
                list=list.subList(0, 1);
            }
            pageable = query.getPageable();
//            long totalElements = query.getTotalElements();
            //问答细节列表
            List<QuestionAnswer> content = query.getContent();
            for (QuestionAnswer questionAnswer : list) {
                questionAnswers.add(questionAnswer);
            }

        }
        //取前十个
        if(questionAnswers.size() > qo.getPageSize()){
            questionAnswers = questionAnswers.subList(0,qo.getPageSize());
        }
        return new PageImpl(questionAnswers,pageable,count);
    }

    @Override
    public List<QuestionAnswer> list() {
        return repository.findAll();
    }
}
